package com.situ.shoplook.admin.api;

import com.situ.shoplook.admin.util.CannotDeleteNodeException;
import com.situ.shoplook.common.model.Product;
import com.situ.shoplook.common.service.ProductService;
import com.situ.shoplook.admin.service.UpLoadFileService;
import com.situ.shoplook.common.util.PaginateInfo;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author:yzy
 * @date:2024/9/2
 */
@RestController
@RequestMapping(value = "/api/product", produces = "application/json;charset=utf-8")
public class ProductApi {
    @DubboReference
    private ProductService productService;
    @Autowired
    private UpLoadFileService uploadFileService;

    //查询全部
    @GetMapping
    public Map<String, Object> findAll(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "2147383647") Integer pageSize,
            @RequestParam(value = "name", required = false) String name
    ) {
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        PaginateInfo pi = new PaginateInfo(pageNo, pageSize);
        Map<String, Object> map = productService.findAll(pi, name);
        List<Product> products = (List<Product>) map.get("data");
        PaginateInfo p = (PaginateInfo) map.get("pi");
        Map<String, Object> map2 =Map.of("total", p.getTotal(), "pageNo", p.getPageNo(), "pageSize", p.getPageSize());

        return Map.of("success",true, "data", products, "pi", map2);
    }

    @GetMapping("/name")
    public Map<String, Object> findByName(@RequestParam(value = "name") String name) {
        List<Product> products = this.productService.findByName(name);
        return Map.of("success", products != null, "data", products);
    }

    @PostMapping
    public Map<String, Object> save(@RequestBody Product product) {
        if (!StringUtils.hasText(product.getName())) {
            return Map.of("success", false, "error", "类别名称不可为空");
        }

        boolean success = this.productService.save(product);
        return Map.of("success", success);
    }

    @PutMapping
    public Map<String, Object> update(@RequestBody Product product) {
        boolean success = this.productService.update(product);
        return Map.of("success", success);
    }

    //富文本编辑器上传图片
    @PostMapping("/editor")
    public Map<String, Object> uploadEditor(@RequestParam("file") MultipartFile file) {
        //对于分布式部署，就应该将图片上传到阿里云，或七牛云，或其它图床
        String url = this.uploadFileService.uploadImage(file, "product/editor");
        return Map.of("errno", 0, "data", Map.of("url", url));
    }

    @DeleteMapping
    public Map<String, Object> deleteByIds(@RequestBody Integer[] ids) {
        System.out.println("ids=" + ids[0]);
        int rows = 0;
        try {
            rows = this.productService.deleteByIds(ids);
        } catch (CannotDeleteNodeException e) {
            return Map.of("success", false);
        }
        return Map.of("rows", rows, "success", rows > 0);
    }
}
